package cn.edu.nuc.fourth.services.impl;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.nuc.fourth.dao.AprojectMapper;
import cn.edu.nuc.fourth.dao.MajorMapper;
import cn.edu.nuc.fourth.dao.ProjectMapper;
import cn.edu.nuc.fourth.dao.SchoolMapper;
import cn.edu.nuc.fourth.dao.StudentMapper;
import cn.edu.nuc.fourth.dao.TeacherMapper;
import cn.edu.nuc.fourth.dto.PageInfo;
import cn.edu.nuc.fourth.model.Aproject;
import cn.edu.nuc.fourth.model.Major;
import cn.edu.nuc.fourth.model.Project;
import cn.edu.nuc.fourth.model.School;
import cn.edu.nuc.fourth.model.SchoolMes;
import cn.edu.nuc.fourth.model.Student;
import cn.edu.nuc.fourth.model.Teacher;
import cn.edu.nuc.fourth.services.interfaces.FillingService;

@Service
public class FillingServiceImpl implements FillingService {

	@Autowired
	private SchoolMapper smapper;
	@Autowired
	private MajorMapper mmapper;
	@Autowired
	private ProjectMapper pmapper;
	@Autowired
	private AprojectMapper amapper;
	@Autowired
	private StudentMapper stumapper;
	@Autowired
	private TeacherMapper tmapper;

	private Integer year = Calendar.getInstance().get(Calendar.YEAR);

	/**
	 * 办学条件填报
	 */
	@Override
	public Integer schoolAdd(School school) {

		School s = smapper.selectByPrimaryKey(school.getSid());
		if (s != null) {

			return -1;
		} else {
			Integer result = smapper.insert(school);
			return result;
		}
	}

	/**
	 * 分页办学显示列表
	 */
	@Override
	public PageInfo schoolList(Integer userid, Integer current) {

		if (current <= 0) {

			current = 1;
		}
		PageInfo pageInfo = new PageInfo(current);
		List<School> list = smapper.findByUserid(userid, pageInfo.getStart(), pageInfo.getOffset());
		Integer count = smapper.count(userid);
		// System.out.println(list);
		// System.out.println(mapper.count(userid));
		pageInfo.setCount(count);
		pageInfo.setList(list);

		return pageInfo;
	}

	/**
	 * 初始化一条办学条件记录
	 */
	@Override
	public School oneSchool(Integer sid) {

		School school = smapper.selectByPrimaryKey(sid);
		return school;
	}

	@Override
	public School schoolEdit(School school) {

		Integer result = smapper.updateByPrimaryKey(school);
		School s = smapper.selectByPrimaryKey(school.getSid());
		return s;
	}

	@Override
	public Integer schoolDelete(Integer sid) {

		Integer result = smapper.deleteByPrimaryKey(sid);
		return result;
	}

	/**
	 * 填报内容不可修改
	 */
	@Override
	public School noeditSchool(Integer sid, Integer state) {

		Integer result = smapper.noedit(sid, state);
		School school = smapper.selectByPrimaryKey(sid);
		return school;
	}

	/**
	 * 提交后的不许填报
	 */
	@Override
	public School noAddSchool(String userid) {

		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		School school = smapper.oneSchool(userid, String.valueOf(year));
		return school;
	}

	///////////
	/**
	 * 专业添加
	 */
	@Override
	public Integer majorAdd(Major major) {

		Integer result = mmapper.insert(major);
		return result;
	}

	/**
	 * 专业分页
	 */
	@Override
	public PageInfo majorList(Integer userid, Integer current) {

		if (current <= 0) {

			current = 1;
		}
		PageInfo pageInfo = new PageInfo(current);
		List<Major> list = mmapper.findByUserid(userid, pageInfo.getStart(), pageInfo.getOffset());
		Integer count = mmapper.count(userid);
		pageInfo.setCount(count);
		pageInfo.setList(list);

		return pageInfo;
	}

	/**
	 * 初始化一条
	 */
	@Override
	public Major oneMajor(Integer majorid) {

		Major major = mmapper.selectByPrimaryKey(majorid);
		return major;
	}

	/**
	 * 执行修改
	 */
	@Override
	public Major majorEdit(Major major) {

		mmapper.updateByPrimaryKey(major);
		Major m = mmapper.selectByPrimaryKey(major.getMajorid());
		return m;
	}

	/**
	 * 填报专业后无法再修改
	 */
	@Override
	public Major noeditMajor(Integer majorid, Integer state) {

		mmapper.noedit(majorid, state);

		Major major = mmapper.selectByPrimaryKey(majorid);
		return major;
	}

	/**
	 * 专业填报之后不能在填报
	 */
	@Override
	public Major noAddMajor(String userid) {
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		Major major = mmapper.noadd(userid, String.valueOf(year));
		return major;
	}

	/**
	 * 删除专业
	 */
	@Override
	public Integer deleteMajor(Integer majorid) {

		Integer result = mmapper.deleteByPrimaryKey(majorid);
		return result;
	}

	/**
	 * 项目填报
	 */
	@Override
	public Integer projectadd(MultipartFile main, MultipartFile province, MultipartFile city, MultipartFile onther,
			Project project, String path) {

		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		project.setDate(String.valueOf(year));
		path += year;
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
		if (!"".equals(main.getOriginalFilename())) {
			project.setProjecttype(main.getName());
			Project p = pmapper.findBytype(project);
			if (p != null) {

				p.setPname(main.getOriginalFilename());
				pmapper.updateByPrimaryKey(p);
				String t = path + main.getOriginalFilename();
				File f = new File(t);
				f.delete();
			} else {
				project.setPname(main.getOriginalFilename());
				project.setProject(path);
				pmapper.insert(project);
			}
			try {
				FileUtils.copyInputStreamToFile(main.getInputStream(), new File(file, main.getOriginalFilename()));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (!"".equals(province.getOriginalFilename())) {

			project.setProjecttype(province.getName());
			Project p = pmapper.findBytype(project);

			if (p != null) {
				p.setPname(province.getOriginalFilename());
				pmapper.updateByPrimaryKey(p);
				String t = path + province.getOriginalFilename();
				File f = new File(t);
				f.delete();

			} else {
				project.setPname(province.getOriginalFilename());
				project.setProject(path);
				pmapper.insert(project);
			}
			try {
				FileUtils.copyInputStreamToFile(province.getInputStream(),
						new File(file, province.getOriginalFilename()));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (!"".equals(city.getOriginalFilename())) {
			project.setProjecttype(city.getName());
			Project p = pmapper.findBytype(project);
			if (p != null) {

				p.setPname(city.getOriginalFilename());
				pmapper.updateByPrimaryKey(p);
				String t = path + city.getOriginalFilename();
				File f = new File(path);
				f.delete();
			} else {
				project.setPname(city.getOriginalFilename());
				project.setProject(path);
				pmapper.insert(project);
			}
			try {
				FileUtils.copyInputStreamToFile(city.getInputStream(), new File(file, city.getOriginalFilename()));
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		if (!"".equals(onther.getOriginalFilename())) {
			project.setProjecttype(onther.getName());
			Project p = pmapper.findBytype(project);

			if (p != null) {
				p.setPname(onther.getOriginalFilename());
				p.setPname(onther.getOriginalFilename());
				pmapper.updateByPrimaryKey(p);
				String t = path + onther.getOriginalFilename();
				File f = new File(path);
				f.delete();

			} else {
				project.setPname(onther.getOriginalFilename());
				project.setProject(path);
				pmapper.insert(project);
			}
			try {
				FileUtils.copyInputStreamToFile(onther.getInputStream(), new File(file, onther.getOriginalFilename()));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 初始化下载用户下载列表
	 */
	@Override
	public Aproject projectlist() {

		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		Aproject aproject = amapper.findBydate(String.valueOf(year));
		return aproject;
	}

	/**
	 * 根据年份查询出的项目填报记录
	 */
	@Override
	public List<Project> dateprojectlist(String userid, String date) {

		List<Project> list = pmapper.findBydate(userid, date);
		return list;
	}

	/**
	 * 删除一条项目记录
	 */
	@Override
	public List<Project> deleteproject(Integer projectid, String userid, String date) {

		Integer result = pmapper.deleteByPrimaryKey(projectid);
		List<Project> list = pmapper.findBydate(userid, date);
		return list;
	}

	/**
	 * 项目修改
	 */
	@Override
	public List<Project> oneupload(MultipartFile file, String path, Project project) {

		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		path += year;
		project.setPname(file.getOriginalFilename());
		project.setProject(path);
		Integer result = pmapper.insert(project);
		List<Project> list = pmapper.findBydate(String.valueOf(project.getUserid()), project.getDate());
		return list;
	}

	/**
	 * 保存后不可修改
	 */
	@Override
	public List<Project> noeditproject(String userid, String date) {

		pmapper.noedit(userid, date);
		List<Project> list = pmapper.findBydate(userid, date);
		return list;
	}

	/**
	 * 报表
	 */
	@Override
	public SchoolMes echart(Integer userid) {

		List<Major> majors = mmapper.echart(userid);
		List<School> schools = smapper.echart(userid);
		SchoolMes sm = new SchoolMes();
		System.out.println(majors.size());
		sm.setSchools(schools);
		sm.setMajors(majors);
		return sm;
	}
	// 这里以下是学生信息填报

	@Override
	public Integer studentadd(Student student) {

		Student stu = stumapper.selectByPrimaryKey(student.getStudentid());
		Integer result = 0;
		if (stu != null) {

			return -1;
		} else {
			result = stumapper.insert(student);
		}
		return result;
	}

	/**
	 * 学生信息保存修改
	 */
	@Override
	public Student studentedit(Student student) {

		stumapper.updateByPrimaryKeySelective(student);
		Student s = stumapper.selectByPrimaryKey(student.getStudentid());
		return s;
	}

	/**
	 * 学生信息分页显示
	 */
	@Override
	public PageInfo studentlist(Integer userid, Integer current) {

		if (current <= 0) {

			current = 1;
		}
		Integer count = stumapper.count(userid);
		PageInfo pageInfo = new PageInfo(current);
		pageInfo.setCount(count);
		List<Student> list = stumapper.page(userid, pageInfo.getStart(), pageInfo.getOffset());
		pageInfo.setList(list);
		pageInfo.setCurrent(current);

		return pageInfo;
	}

	/**
	 * 初始化一条学生信息的数据
	 */
	@Override
	public Student onestudent(Integer studentid) {

		Student student = stumapper.selectByPrimaryKey(studentid);
		return student;
	}

	@Override
	public Student noeditstudent(Integer studentid, Integer state) {

		Student student = new Student();
		student.setStudentid(studentid);
		student.setState(state);
		stumapper.updateByPrimaryKeySelective(student);
		Student s = stumapper.selectByPrimaryKey(studentid);
		return s;
	}

	@Override
	public Student noAddStudent(Integer userid) {
		System.out.println(year);
		Student student = stumapper.findByDate(userid, String.valueOf(year));
		return student;
	}
	/// 以下是教师信息

	/**
	 * 教师添加
	 */
	@Override
	public Integer teacheradd(Teacher teacher) {

		Teacher t = tmapper.selectByPrimaryKey(teacher.getTeacherid());
		Integer result = 0;
		if (t != null) {

			return -1;
		} else {
			result = tmapper.insert(teacher);
		}
		return result;
	}

	@Override
	public PageInfo teacherlist(Integer userid, Integer current) {

		if (current <= 0) {

			current = 1;
		}
		PageInfo pageInfo = new PageInfo(current);
		Integer count = tmapper.count(userid);
		pageInfo.setCount(count);
		List<Teacher> list = tmapper.page(userid, pageInfo.getStart(), pageInfo.getOffset());
		pageInfo.setList(list);
		pageInfo.setCurrent(current);
		return pageInfo;
	}

	@Override
	public Student teacheredit(Teacher teacher) {
		return null;
	}

	@Override
	public Student oneteacher(Integer teacherid) {
		return null;
	}

	@Override
	public Student noeditteacher(Integer teacherid, Integer state) {
		return null;
	}

	@Override
	public Student noAddTeacher(Integer userid) {
		return null;
	}

}
